/**
 * 
 */
package me.lc.linklinkchrist.sprite;

import me.lc.linklinkchrist.core.GameSystem;

import com.wiyun.engine.nodes.AtlasLabel;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 游戏进度块
 * 
 * @author Administrator
 */
public class ProcessBlock extends Node {

	Sprite sptProcessBG;
	Sprite sptProcessFore;
	AtlasLabel labelNum;

	int currentSeconds = 90;
	int totalSeconds = 90;

	static float NUM_WIDTH = ResolutionIndependent.resolveDp(30);
	static float NUM_HEIGHT = ResolutionIndependent.resolveDp(40);
	static float NUM_StartX = ResolutionIndependent.resolveDp(245);
	static float NUM_StartY = ResolutionIndependent.resolveDp((40 - 25) / 2);

	public ProcessBlock() {
		sptProcessBG = Sprite.make(GameSystem.processBgTex);
		sptProcessBG.setAnchorPercent(0, 0);
		sptProcessBG.setPosition(0, 0);
		sptProcessFore = Sprite.make(GameSystem.processForeTex);
		sptProcessFore.setAnchorPercent(0, 0);
		sptProcessFore.setPosition(0, 0);
		sptProcessFore.autoRelease(true);

		labelNum = AtlasLabel.make("90", GameSystem.numberTex,
				GameSystem.charMap);
		labelNum.setAnchorPercent(0, 0);
		labelNum.setColor(new WYColor3B(255, 0, 0));
		labelNum.setPosition(NUM_StartX, NUM_StartY);

		addChild(sptProcessBG);
		addChild(sptProcessFore);
		addChild(labelNum);

		setPosition();
	}

	private void setPosition() {
		float processWidth = GameSystem.processForeTex.getWidth()
				+ GameSystem.numberTex.getWidth() / 5.0f;
		float leftMargin = (GameSystem.Screen_Width - processWidth) / 2.0f;
		float height = GameSystem.Screen_Height
				- ResolutionIndependent.resolveDp(GameSystem.BLOCK_SIZE);
		this.setAnchorPercent(0, 0);
		this.setPosition(WYPoint.make(leftMargin, height));
	}

	/**
	 * 初始化进度条
	 * 
	 * @param totalSecond
	 */
	public void initProcessBlock(int totalSecond) {
		totalSeconds = totalSecond;
		currentSeconds = totalSecond;
		updateProcess(totalSecond);
	}

	/**
	 * 更新进度
	 * 
	 * @param current
	 */
	public void updateProcess(int current) {
		labelNum.setText(String.format("%02d", current));
		// 减少进度条
		currentSeconds = current;
		removeChild(sptProcessFore, true);
		sptProcessFore = Sprite.make(
				GameSystem.processForeTex,
				WYRect.make(0, 0, GameSystem.processBgTex.getWidth()
						* (currentSeconds / (float) totalSeconds), NUM_HEIGHT));
		sptProcessFore.setAnchorPercent(0, 0);
		sptProcessFore.setPosition(0, 0);
		// sptProcessFore.autoRelease(true);
		addChild(sptProcessFore);
		// sptProcessFore.setClipRect(WYRect.make(0, 0,
		// GameSystem.processBgTex.getWidth()
		// * (currentSeconds / (float) totalSeconds), NUM_HEIGHT));
	}
}
